package iscript.coreFramework.displays 
{
	import com.greensock.loading.core.DisplayObjectLoader;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	import iscript.coreFramework.math.Point2D;

	public class Element 
	{
		
		public function Element() 
		{
			
		}
		
		//{#region  ================清除显示对象===================
         /**
	     * 删除全部 Object中的子对象
	     */
		static public function removeAllItem(container:DisplayObjectContainer):void {
			if (container == null) { return; }
			for(var i:int=container.numChildren-1; i>=0; i--) {
				container.removeChildAt(0);
			}
		}
		
		static public function remove(container:DisplayObject):void {
			   if (container.parent == null) return;
			   container.parent.removeChild(container);
			}
		
		/**
		 * 删除一个显示对象
		 * @param	ContainerObj
		 */
		static public function clear(target1:DisplayObjectContainer,target2:DisplayObject):void {
			//try{ContainerObj.parent.removeChild(ContainerObj);}catch(e:*){}	
		 	if (target1 == null || target2 == null) { return; }
			  try{target1.removeChild(target2);}catch(e:*){}		
			}
         
		 
		/**
		 * 删除Zindex位置的显示对象  或者 Zindex  EndZindex 范围的 显示对象
		 * @param	ContainerObj
		 * @param	Zindex
		 * @param	EndZindex
		 */
		static public function removeByZindex(ContainerObj:*, StartZindex:int, EndZindex:*= null):void {	
			if (ContainerObj == null) { return; }
			var num:int = StartZindex;
			if (EndZindex != null) {
				var _temp_:Number;
				EndZindex > ContainerObj.numChildren - 1?_temp_ = ContainerObj.numChildren - 1:_temp_ = EndZindex;

				for (var  i:int = _temp_; i >= num; i--) {
					try{ContainerObj.removeChild(ContainerObj.getChildAt(i))}catch(e:*){}					
					}
				}else {
		     	if(ContainerObj.numChildren-1>=num){
			      try{ContainerObj.removeChild(ContainerObj.getChildAt(num))}catch(e:*){}
		         	}		
				}
			}
		
		/**
		 * 删除类型相符的子对象
		 * type String  "[object Shape]"
		 */
		static public function removeByType(ContainerObj:*, type:String):void {
			if (ContainerObj == null) { return; }
			var objArr:Array = new Array();
			for (var i:int = 0; i < ContainerObj.numChildren; i++) {
				if (type!=null) {
					if (ContainerObj.getChildAt(i).toString().toUpperCase()==type.toUpperCase()) {
						objArr.push(ContainerObj.getChildAt(i).name);
					} 
				}
			}	
			for (var d:int=0; d<objArr.length; d++) {
				 try{ContainerObj.removeChild(ContainerObj.getChildByName(objArr[d]));}catch(e:*){}
			}
			objArr=null;
		}
		
		/**
		 * 删除名字相符的子对象 Array多个子对象  包含对^ 名字的过滤使用
		 * @param	ContainerObj
		 * @param	...arg
		 */
		static public function removeByName(ContainerObj:*, ...arg):void { 
			if (arg.length == 1 && arg[0].substring(0, 1) == "^") {
		 
				var objArr:Array = new Array();
				var names:String = arg[0].substring(1);
				for (var j:int = 0; j < ContainerObj.numChildren; j++) {				 
					if (ContainerObj.getChildAt(j).name.substring(0,names.length)==arg[0].substring(1)) {
						objArr.push(ContainerObj.getChildAt(j).name);
					} 
			     }
			    for (var d:int = 0; d < objArr.length; d++) {
					try{
				    ContainerObj.removeChild(ContainerObj.getChildByName(objArr[d]));
					}catch(e:*){}
			       }
				objArr=null;
				return;
				}
			for (var i:int = 0; i < arg.length; i++) {
					
				try {
					
				ContainerObj.removeChild(ContainerObj.getChildByName(arg[i]))
				
				}catch(e:*){}
				}			
			}
	//}#endregion
			
		/**
		 * 根据name获得 显示对象列表  包含对^ 的过滤
		 * @param	ContainerObj
		 * @param	...arg
		 * @return
		 */
			static public function getDomByName(ContainerObj:*, ...arg):Array {					
				var objArr:Array = new Array();
				if (arg.length == 1 && arg[0].substring(0, 1) == "^") {
				var names:String = arg[0].substring(1);
				for (var j:int = 0; j < ContainerObj.numChildren; j++) {	
					if (ContainerObj.getChildAt(j).name.substring(0,names.length)==arg[0].substring(1)) {
						objArr.push(ContainerObj.getChildAt(j));
					} 
			     } 
				}else {
					for ( j = 0; j < ContainerObj.numChildren; j++) {	
					if (ContainerObj.getChildAt(j).name == arg[0] ) {
						objArr.push(ContainerObj.getChildAt(j));
					} 
			      }
				}
				return objArr
				}
		
		/**
		 * 根据"[object Shape]"获得 显示对象列表  包含对^ 的过滤
		 * @param	ContainerObj
		 * @param	type
		 * @return
		 */
			static public function getDomByType(ContainerObj:*, type:String):Array {
				var objArr:Array = new Array();
			     for (var i:int = 0; i < ContainerObj.numChildren; i++) {
				    if (type != null) {
						//trace(ContainerObj.getChildAt(i).toString().toUpperCase())
					if (ContainerObj.getChildAt(i).toString().toUpperCase()==type.toUpperCase()) {
						objArr.push(ContainerObj.getChildAt(i));
					} 
				   }
			     }	
				return objArr;
				}
				
			static public function swapChildrenTotop(child1:DisplayObject, child2:DisplayObject):void {
			   if (child1.parent.getChildIndex(child1) < child1.parent.getChildIndex(child2)) {
				      child1.parent.swapChildren(child1,child2)
				   }
			}
			
			static public function setPosition(target:DisplayObject, pos:Point2D):void {
			  target.x = pos.x;
			  target.y = pos.y;			
			}
			
			static public function setRectangle(target:DisplayObject,rect:Rectangle):void {
				  target.x = rect.x;
			      target.y = rect.y;
				  target.width = rect.width;
				  target.height = rect.height;
				}
			
			static public function getClass(source:Loader=null,libClassName:String=null,displayObjectType:String="MovieClip"):*{
			var myClass:Class;
				if(source!=null){
				  if(source.contentLoaderInfo.applicationDomain.hasDefinition(libClassName)){
					myClass= source.contentLoaderInfo.applicationDomain.getDefinition(libClassName) as Class;}
				  else{return myClass}	
				}else{
				  if(ApplicationDomain.currentDomain.hasDefinition(libClassName)){
					myClass= getDefinitionByName(libClassName) as Class; 
				  }else{return myClass}			  
				}
				 switch(displayObjectType.toLocaleUpperCase()){
				   case "BITMAPDATA":
					   return  new myClass(0, 0) as BitmapData;
					   break;
				   case "BITMAP":
					   return  new Bitmap(new myClass(0, 0) as BitmapData);
					   break;
				   case "TEXTFIELD":
					   return  new myClass() as TextField;
					   break;
				   case "MOVIECLIP":
					   return  new myClass() as MovieClip;
					   break;
				   case "SPRITE":
					   return  new myClass() as Sprite;
					   break;
				   case "SHAPE":
					   return  new myClass() as Shape;
					   break;			 
				 }
		    }
			
			static public function localToGlobal(target:DisplayObject):Point {
				   if (target.parent == null) return  new Point( -500, -500); 
				   return  target.parent.localToGlobal(new Point(target.x, target.y));
				}
			
			static public function drag(target:DisplayObject, rect:Rectangle):void {
				DragObject.getInstance().add(target, rect);
				}
			
			static public function isInStage(target:DisplayObject):Boolean {
				   	 return  target.parent != null;
				}
			 
			static public function  unActive(target:DisplayObjectContainer):void {
				  target.mouseChildren = false;
				  target.mouseEnabled = false;
				  target.tabEnabled = false;
				  target.tabChildren = false;
				}
			static public function  Active(target:DisplayObjectContainer):void {
				  target.mouseChildren = true;
				  target.mouseEnabled = true;
				  target.tabEnabled = true;
				  target.tabChildren = true;
				}
	}

}